import {Toast} from 'vant'
import app from '../main'

export function isEmail(str: string) {
    const reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    return reg.test(str);
}

export function copyDomText(val: string): void {
    // 获取需要复制的元素以及元素内的文本内容
    const text = val
    // 添加一个input元素放置需要的文本内容
    const input = document.createElement('input')
    input.value = text
    document.body.appendChild(input)
    // 选中并复制文本到剪切板
    input.select()
    document.execCommand('copy')
    // 移除input元素
    document.body.removeChild(input)
    Toast(app.config.globalProperties.$t('all.yifuzhi'))
}

export function formatDate(date: Date, fmt: string) {
    //获取年份
    if (/(y+)/.test(fmt)) {
        // 把数字变成字符串
        const dateY = date.getFullYear() + "";
        //RegExp.$1 在判断中出现过，且是括号括起来的，所以 RegExp.$1 就是 "yyyy"
        fmt = fmt.replace(RegExp.$1, dateY.substr(4 - RegExp.$1.length));
    }

    //获取其他
    const o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
    };
    for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            const str = (o as any)[k] + "";
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1 ? str : (("00" + str).substr(str.length))
            );
        }
    }
    return fmt;
}

export const chainList = {
    eth: {
        name: 'ETH',
        icon: require('@/assets/images/erc.png'),
        rpc_url: 'https://eth.llamarpc.com',
        usdt_address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
        usdt_wei: 6,
        usdt_text_address: '0xB6e83B80767C8Fc019F49e440D5ff1d5788B8dD9',
        usdt_text_wei: 18,
    },
    trx: {
        name: 'TRX',
        icon: require('@/assets/images/trc.png'),
        usdt_address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
        usdt_wei: 6,
        usdt_text_address: 'TXLAQ63Xg1NAzckPwKHvzw7CSEmLMEqcdj',
        usdt_text_wei: 18,
    },
    bsc: {
        name: 'BSC',
        icon: require('@/assets/images/bsc.png'),
        rpc_url: 'https://bsc.mytokenpocket.vip',
        usdt_address: '0x55d398326f99059fF775485246999027B3197955',
        usdt_wei: 18,
        usdt_text_address: '0xeD24FC36d5Ee211Ea25A80239Fb8C4Cfd80f12Ee',
        usdt_text_wei: 18,
    }
}


export function formatMoney(s: any, type = 2) {
    if (type != 11) {
        try {
            if (type == 10) {
                type = +(s.toString().split(".")[1].length);
            }
        } catch (error) {
            type = 2
        }
    } else {
        type = 0
    }

    const ret: any = (+s).toLocaleString(undefined, {maximumFractionDigits: type, minimumFractionDigits: type})
    if (ret <= 0) {
        return '--'
    } else {
        return ret
    }
}

// 科学计数法转数字
export function toNonExponential(num: any) {
    const m = num.toExponential().match(/\d(?:.(\d*))?e([+-]\d+)/);
    return num.toFixed(Math.max(0, (m[1] || "").length - m[2]));
}
